// ************************************************************************************
// Table 3: Robustness checks for sample selection, data quality, and CMA fixed effects
// ************************************************************************************

// Writes the results as reg_robust_parcel and reg_robust_ratio to $resultpath/tables/

use "$outpath/database_for_reg.dta", clear

eststo clear

// Checks for parcels

global instruct "tdec(3) rdec(3) auto(3) bdec (3) symbol($^a$,$^b$,$^c$) se e(r2_p) tex label nocons"

// Benchmark
xi: reghdfe Lnland_pwc Lnpopcma Lnareacma dist_min_NEW i.cat0 spacematep spacematep2 spacematep3 spacematep4 Lnemp i.hoffice i.exports NBdiffbusiness NBdiffnaics4 NBdiffproduct Lndist_maj_air Lndist_maj_sea Lndist_stat_freight Lndist_junction if samplep==1, absorb(naics4d eruid) cluster(cmauid)
outreg2 using "$resultpath/tables/reg_robust_parcel.xls", replace $instruct

// No restriction on quality of assignment
xi: reghdfe Lnland_pwc Lnpopcma Lnareacma dist_min_NEW i.cat0 spacematep spacematep2 spacematep3 spacematep4 Lnemp i.hoffice i.exports NBdiffbusiness NBdiffnaics4 NBdiffproduct Lndist_maj_air Lndist_maj_sea Lndist_stat_freight Lndist_junction if exports!=.&year==2017&cmauid!=., absorb(naics4d eruid) cluster(cmauid)
outreg2 using "$resultpath/tables/reg_robust_parcel.xls", append $instruct

// Drop extreme values of parcel size per worker
egen p1=pctile(Lnland_pwc) if samplep==1, p(1)
egen p99=pctile(Lnland_pwc) if samplep==1, p(99)

xi: reghdfe Lnland_pwc Lnpopcma Lnareacma dist_min_NEW i.cat0 spacematep spacematep2 spacematep3 spacematep4 Lnemp i.hoffice i.exports NBdiffbusiness NBdiffnaics4 NBdiffproduct Lndist_maj_air Lndist_maj_sea Lndist_stat_freight Lndist_junction if samplep==1&Lnland_pwc>p1&Lnland_pwc<p99, absorb(naics4d eruid) cluster(cmauid)
outreg2 using "$resultpath/tables/reg_robust_parcel.xls", append $instruct
drop p1 p99

// Restrict to observations with information of excellent quality on both parcel and building footprint
xi: reghdfe Lnland_pwc Lnpopcma Lnareacma dist_min_NEW i.cat0 spacematep spacematep2 spacematep3 spacematep4 Lnemp i.hoffice i.exports NBdiffbusiness NBdiffnaics4 NBdiffproduct Lndist_maj_air Lndist_maj_sea Lndist_stat_freight Lndist_junction if samplep==1&sampleb==1, absorb(naics4d eruid) cluster(cmauid)
outreg2 using "$resultpath/tables/reg_robust_parcel.xls", append $instruct

// Restrict to observations for which parcel size is bigger than building footprint
xi: reghdfe Lnland_pwc Lnpopcma Lnareacma dist_min_NEW i.cat0 spacematep spacematep2 spacematep3 spacematep4 Lnemp i.hoffice i.exports NBdiffbusiness NBdiffnaics4 NBdiffproduct Lndist_maj_air Lndist_maj_sea Lndist_stat_freight Lndist_junction if samplep==1&P_area_c>=B_area_c&P_area_c!=.&B_area_c!=.,absorb(naics4d eruid) cluster(cmauid)
outreg2 using "$resultpath/tables/reg_robust_parcel.xls", append $instruct

// Restrict to observations with less than 50 employeees
xi: reghdfe Lnland_pwc Lnpopcma Lnareacma dist_min_NEW i.cat0 spacematep spacematep2 spacematep3 spacematep4 Lnemp i.hoffice i.exports NBdiffbusiness NBdiffnaics4 NBdiffproduct Lndist_maj_air Lndist_maj_sea Lndist_stat_freight Lndist_junction if samplep==1&Lnemp<log(50),absorb(naics4d eruid) cluster(cmauid)
outreg2 using "$resultpath/tables/reg_robust_parcel.xls", append $instruct

// Restrict to observations with no neighbor
xi: reghdfe Lnland_pwc Lnpopcma Lnareacma dist_min_NEW i.cat0 spacematep spacematep2 spacematep3 spacematep4 Lnemp i.hoffice i.exports NBdiffbusiness NBdiffnaics4 NBdiffproduct Lndist_maj_air Lndist_maj_sea Lndist_stat_freight Lndist_junction if samplep==1&spacematep==0, absorb(naics4d eruid) cluster(cmauid)
outreg2 using "$resultpath/tables/reg_robust_parcel.xls", append $instruct

// Replace number of neighbors on the parcel by the average of
// the number of neighbors on the parcel and in the building
foreach i in "" "2" "3" "4"{
	gen spacematem`i'=0.5*(spacematep`i'+spacemateb`i')
	replace spacematem`i'=spacematep`i' if spacematem`i'==.&spacematep`i'!=.
	replace spacematem`i'=spacemateb`i' if spacematem`i'==.&spacemateb`i'!=.
}

xi: reghdfe Lnland_pwc Lnpopcma Lnareacma dist_min_NEW i.cat0 spacematem spacematem2 spacematem3 spacematem4 Lnemp i.hoffice i.exports NBdiffbusiness NBdiffnaics4 NBdiffproduct Lndist_maj_air Lndist_maj_sea Lndist_stat_freight Lndist_junction if samplep==1, absorb(naics4d eruid) cluster(cmauid)
outreg2 using "$resultpath/tables/reg_robust_parcel.xls", append $instruct

// Restrict to observations that are more than 5km away from closest city centre
xi: reghdfe Lnland_pwc Lnpopcma Lnareacma dist_min_NEW i.cat0 spacematep spacematep2 spacematep3 spacematep4 Lnemp i.hoffice i.exports NBdiffbusiness NBdiffnaics4 NBdiffproduct Lndist_maj_air Lndist_maj_sea Lndist_stat_freight Lndist_junction if samplep==1&dist_min_NEW>=5, absorb(naics4d eruid) cluster(cmauid)
outreg2 using "$resultpath/tables/reg_robust_parcel.xls", append $instruct

// Include CMA fixed effect
xi: reghdfe Lnland_pwc dist_min_NEW i.cat0 spacematep spacematep2 spacematep3 spacematep4 Lnemp i.hoffice i.exports NBdiffbusiness NBdiffnaics4 NBdiffproduct Lndist_maj_air Lndist_maj_sea Lndist_stat_freight Lndist_junction if samplep==1, absorb(naics4d cmauid) cluster(cmauid)
outreg2 using "$resultpath/tables/reg_robust_parcel.xls", append $instruct

// Checks for ratio

global instruct "tdec(3) rdec(3) auto(3) bdec (3) symbol($^a$,$^b$,$^c$) se e(r2_p) tex label nocons"

// Benchmark
xi: reghdfe ratio Lnpopcma Lnareacma dist_min_NEW i.cat0 spacematep spacematep2 spacematep3 spacematep4 Lnemp i.hoffice i.exports NBdiffbusiness NBdiffnaics4 NBdiffproduct Lndist_maj_air Lndist_maj_sea Lndist_stat_freight Lndist_junction if samplep==1, absorb(naics4d eruid) cluster(cmauid)
outreg2 using "$resultpath/tables/reg_robust_ratio.xls", replace $instruct

// No restriction on quality of assignment
xi: reghdfe ratio Lnpopcma Lnareacma dist_min_NEW i.cat0 spacematep spacematep2 spacematep3 spacematep4 Lnemp i.hoffice i.exports NBdiffbusiness NBdiffnaics4 NBdiffproduct Lndist_maj_air Lndist_maj_sea Lndist_stat_freight Lndist_junction if exports!=.&year==2017&cmauid!=., absorb(naics4d eruid) cluster(cmauid)
outreg2 using "$resultpath/tables/reg_robust_ratio.xls", append $instruct

// Drop extreme values of parcel size per worker
egen p1=pctile(ratio) if samplep==1, p(1)
egen p99=pctile(ratio) if samplep==1, p(99)

xi: reghdfe ratio Lnpopcma Lnareacma dist_min_NEW i.cat0 spacematep spacematep2 spacematep3 spacematep4 Lnemp i.hoffice i.exports NBdiffbusiness NBdiffnaics4 NBdiffproduct Lndist_maj_air Lndist_maj_sea Lndist_stat_freight Lndist_junction if samplep==1&ratio>p1&ratio<p99, absorb(naics4d eruid) cluster(cmauid)
outreg2 using "$resultpath/tables/reg_robust_ratio.xls", append $instruct
drop p1 p99

// Restrict to observations with information of excellent quality on both parcel and building footprint
xi: reghdfe ratio Lnpopcma Lnareacma dist_min_NEW i.cat0 spacematep spacematep2 spacematep3 spacematep4 Lnemp i.hoffice i.exports NBdiffbusiness NBdiffnaics4 NBdiffproduct Lndist_maj_air Lndist_maj_sea Lndist_stat_freight Lndist_junction if samplep==1&sampleb==1, absorb(naics4d eruid) cluster(cmauid)
outreg2 using "$resultpath/tables/reg_robust_ratio.xls", append $instruct

// Restrict to observations for which parcel size is bigger than building footprint
xi: reghdfe ratio Lnpopcma Lnareacma dist_min_NEW i.cat0 spacematep spacematep2 spacematep3 spacematep4 Lnemp i.hoffice i.exports NBdiffbusiness NBdiffnaics4 NBdiffproduct Lndist_maj_air Lndist_maj_sea Lndist_stat_freight Lndist_junction if samplep==1&P_area_c>=B_area_c&P_area_c!=.&B_area_c!=.,absorb(naics4d eruid) cluster(cmauid)
outreg2 using "$resultpath/tables/reg_robust_ratio.xls", append $instruct

// Restrict to observations with less than 50 employeees
xi: reghdfe ratio Lnpopcma Lnareacma dist_min_NEW i.cat0 spacematep spacematep2 spacematep3 spacematep4 Lnemp i.hoffice i.exports NBdiffbusiness NBdiffnaics4 NBdiffproduct Lndist_maj_air Lndist_maj_sea Lndist_stat_freight Lndist_junction if samplep==1&year==2017&Lnemp<log(50),absorb(naics4d eruid) cluster(cmauid)
outreg2 using "$resultpath/tables/reg_robust_ratio.xls", append $instruct

// Restrict to observations with no neighbor
xi: reghdfe ratio Lnpopcma Lnareacma dist_min_NEW i.cat0 spacematep spacematep2 spacematep3 spacematep4 Lnemp i.hoffice i.exports NBdiffbusiness NBdiffnaics4 NBdiffproduct Lndist_maj_air Lndist_maj_sea Lndist_stat_freight Lndist_junction if samplep==1&spacematep==0, absorb(naics4d eruid) cluster(cmauid)
outreg2 using "$resultpath/tables/reg_robust_ratio.xls", append $instruct

// Replace number of neighbors on the parcel by the average of
// the number of neighbors on the parcel and in the buildings
xi: reghdfe ratio Lnpopcma Lnareacma dist_min_NEW i.cat0 spacematem spacematem2 spacematem3 spacematem4 Lnemp i.hoffice i.exports NBdiffbusiness NBdiffnaics4 NBdiffproduct Lndist_maj_air Lndist_maj_sea Lndist_stat_freight Lndist_junction if samplep==1, absorb(naics4d eruid) cluster(cmauid)
outreg2 using "$resultpath/tables/reg_robust_ratio.xls", append $instruct

// Restrict to observations that are more than 5km away from closest city centre
xi: reghdfe ratio Lnpopcma Lnareacma dist_min_NEW i.cat0 spacematep spacematep2 spacematep3 spacematep4 Lnemp i.hoffice i.exports NBdiffbusiness NBdiffnaics4 NBdiffproduct Lndist_maj_air Lndist_maj_sea Lndist_stat_freight Lndist_junction if samplep==1&dist_min_NEW>=5, absorb(naics4d eruid) cluster(cmauid)
outreg2 using "$resultpath/tables/reg_robust_ratio.xls", append $instruct

// Include CMA fixed effect
xi: reghdfe ratio dist_min_NEW i.cat0 spacematep spacematep2 spacematep3 spacematep4 Lnemp i.hoffice i.exports NBdiffbusiness NBdiffnaics4 NBdiffproduct Lndist_maj_air Lndist_maj_sea Lndist_stat_freight Lndist_junction if samplep==1, absorb(naics4d cmauid) cluster(cmauid)
outreg2 using "$resultpath/tables/reg_robust_ratio.xls", append $instruct
